// miniprogram/pages/info/zmyygzs/zmyygzs.js
var app = getApp();
var util = require('../../../utils/util.js');
var input_id, zy_memo, ticketv = "进出站";
var gzry_dep = "", gzry_zqdd = "", gzry_name = "", gzryphone, frist_on = "首次登记人员",  twvalue, select_value;
var name ,  sfz ,  sex, phone, address, lxbx, lxbx04zhengzhuang, lxbx04other1, workaddress,  workphone, startwhere,
  crosswhere, fromwhere, traininfo, gzry_memo,  shiyou ;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lxbx04zhengzhuangH: true,
    hsfwc: true,
    zmyymz_fz_tw_moda:true,
    ticket: false,
    items: [
      { name: '进出站人员', value: '进出站人员' ,checked:true},
      { name: '买票/问事', value: '买票/问事' },
    ], 
 
  }, 
 
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {

    this.zmyygzs_lxbx();

    this.lxbx04();
    this.sex_radio();
    this.findOpenid();
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../../chooseLib/chooseLib',
      })
      return
    }

  },

  //拍照或者从相册选择要识别的照片
  photo(type) {
    let that = this

    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], ///'original',压缩图
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        let imgUrl = res.tempFilePaths[0];

        that.uploadImg(app.globalData.openid + new Date().getTime(), imgUrl)

      }

    })

  },

  // 上传图片到云存储
  uploadImg(type, imgUrl) {

    let that = this
    wx.cloud.uploadFile({
      cloudPath: 'ocr/' + type + '.png',
      filePath: imgUrl, // 文件路径
      success: res => {
        // console.log("上传成功", res.fileID)
        that.getImgUrl(type, res.fileID)
      },
      fail: err => {
        // console.log("上传失败", err)
      }
    })

  },

  //获取云存储里的图片url
  getImgUrl(type, imgUrl) {
    wx.showToast({
      title: '正在识别',
      icon: 'loading'
    })
    let that = this
    wx.cloud.getTempFileURL({
      fileList: [imgUrl],
      success: res => {
        let imgUrl = res.fileList[0].tempFileURL
        // console.log("获取图片url成功", imgUrl)
        that.shibie(type, imgUrl)
      },
      fail: err => {
        // console.log("获取图片url失败", err)
      }
    })
  },

  //调用云函数，实现OCR识别
  shibie(type, imgUrl) {
    // console.log("识别成功shibie", type)
    let that = this
    wx.cloud.callFunction({
      name: "photo",
      data: {
        type: "shenfenzheng",
        imgUrl: imgUrl
      },
      success(res) {

        switch (res.result.gender) {

          case "男":
            that.setData({
              name: res.result.name,
              sfz: res.result.id,
              address: res.result.addr,
              sex_radio: [{ checked: true, anme: "男", value: "男" },
              { checked: false, anme: "女", value: "女" }]
            })
            break;
          case "女":
            that.setData({
              name: res.result.name,
              sfz: res.result.id,
              address: res.result.addr,
              sex_radio: [{ checked: false, anme: "男", value: "男" },
              { checked: true, anme: "女", value: "女" }]
            })
            break;
        }


      },
      fail(res) {
        // console.log("识别失败", res)
      }
    })
    // console.log("识别成功res.result.name")


  },
  //确定按钮点击事件,门诊体温
  zmyymz_tw_en: function () {
    // console.log('确定按钮点击事件,门诊体温') 
    this.add_mz_ms();
    this.setData({
      zmyymz_fz_tw_moda: true,
    })
  },
  //取消按钮点击事件,门诊体温
  zmyymz_tw_ca: function () {

    this.setData({
      zmyymz_fz_tw_moda: true,
    })
  }, 
  zmyymz_twvalue_check: function (e) {
    if (parseFloat(twvalue) > 34 && parseFloat(twvalue) < 41) {
      // console.log("输出1：", parseFloat(twvalue))
      if (parseFloat(twvalue) >= 37) {
        console.log("体温高", parseFloat(twvalue))
        this.setData({
          zmyymz_fz_tw_moda: false,
        })
      }
      else {
        console.log("体温正常", parseFloat(twvalue))
        this.add_mz_ms();

      }
    }
    else {
      // console.log("不在范围", parseFloat(twvalue))
      wx.showToast({
        title: '体温不对请重测',
        icon: 'success'
      })
    }
  },
  switch1Change :function(e){
    console.log('马上查询数据库，switch1Change:',e.detail.value)
    if (e.detail.value =="进出站人员"){
      ticketv = "进出站"
      this.setData({
        ticket:false,
      
      })
    } 
    else
    {
      ticketv = "买票"
      this.setData({
        ticket: true,
        startwhere:"",
        crosswhere:"",
        fromwhere:"",
        traininfo:""

      })
    }
    console.log('马上查询数据库，ticketv:', ticketv)
  }
 ,
  select_person: function () {
    const db = wx.cloud.database();
    db.collection('info_zmyy').where({
      phone: select_value,
      form: "火车站测温台"
    }).limit(1)
      .orderBy('gzry_addtime', 'desc')//按字段排序
      .get().then(res => {

        // console.log('查询数据库，data:', res.data)
        if (res.data.length) {
          wx.showToast({
            title: '信息已填充',
            icon: 'success',
          })
          frist_on = "再次登记人员"
          twvalue = ""
          // console.log('查询数据库，data:', res.data[0])
          let arrlxbx = [], arrlxbx04 = [], arrsex = [];  //存放选中id的数组
          let json;
          try {
            if (res.data[0].lxbx != "") {
              for (let i = 0; i < this.data.zmyygzs_lxbx.length; i++) {
                if (res.data[0].lxbx.includes(this.data.zmyygzs_lxbx[i].value)) {
                  // 全选获取选中的值
                  json = { "checked": true, "name": this.data.zmyygzs_lxbx[i].name, "value": this.data.zmyygzs_lxbx[i].value }
                  // arrlxbxv.push(json.value)
                }
                else {
                  json = { "checked": false, "name": this.data.zmyygzs_lxbx[i].name, "value": this.data.zmyygzs_lxbx[i].value }
                }
                arrlxbx.push(json);

              }
            }
            else {
              for (let i = 0; i < this.data.zmyygzs_lxbx.length; i++) {

                json = { "checked": false, "name": this.data.zmyygzs_lxbx[i].name, "value": this.data.zmyygzs_lxbx[i].value }
                arrlxbx.push(json);
              }
            }
            this.setData({
              zmyygzs_lxbx: arrlxbx,
            })
          } catch {

          }


          try {
            if (res.data[0].sex != "") {
              for (let i = 0; i < this.data.sex_radio.length; i++) {
                if (res.data[0].sex.includes(this.data.sex_radio[i].name)) {
                  // 全选获取选中的值
                  json = { "checked": true, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
                }
                else {
                  json = { "checked": false, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
                }
                arrsex.push(json);
              }
            }
            else {
              for (let i = 0; i < this.data.sex_radio.length; i++) {
                json = { "checked": false, "name": this.data.sex_radio[i].name, "value": this.data.sex_radio[i].value }
                arrsex.push(json);
              }
            }
            this.setData({
              sex_radio: arrsex,

            })
          } catch {
          }

          try {
            if (res.data[0].lxbx04zhengzhuang != "") {

              this.setData({
                lxbx04zhengzhuangH: false,
              })
              for (let i = 0; i < this.data.lxbx04zhengzhuang.length; i++) {
                if (res.data[0].lxbx04zhengzhuang.includes(this.data.lxbx04zhengzhuang[i].value)) {
                  // 全选获取选中的值
                  json = { "checked": true, "name": this.data.lxbx04zhengzhuang[i].name, "value": this.data.lxbx04zhengzhuang[i].value }
                }
                else {
                  json = { "checked": false, "name": this.data.lxbx04zhengzhuang[i].name, "value": this.data.lxbx04zhengzhuang[i].value }
                }
                arrlxbx04.push(json);
              }
            }
            else {
              this.setData({
                lxbx04zhengzhuangH: true,
              })
              for (let i = 0; i < this.data.lxbx04zhengzhuang.length; i++) {
                json = { "checked": false, "name": this.data.lxbx04zhengzhuang[i].name, "value": this.data.lxbx04zhengzhuang[i].value }
                arrlxbx04.push(json);
              }
            }
            this.setData({
              lxbx04zhengzhuang: arrlxbx04,
            })
          } catch {

          }


          
          this.setData({
            name: res.data[0].name,
            sex: res.data[0].sex,
            sfz: res.data[0].sfz,
            address: res.data[0].address,
            phone: res.data[0].phone,
            lxbx04other1: res.data[0].lxbx04other1,
            workaddress: res.data[0].workaddress,
            workphone: res.data[0].workphone,
            startwhere: res.data[0].startwhere,
            crosswhere: res.data[0].crosswhere,
            fromwhere: res.data[0].fromwhere,
            traininfo: res.data[0].traininfo,
            memo: res.data[0].gzry_memo,
          })
          // 下面住院信息用



        }
        else {
          wx.showToast({
            title: '无就诊记录',
            icon: 'success',
          })
          twvalue = ""
          this.setData({
            phone: select_value
          })
        }
      })


     
  },
  
  lxbx04: function (e) {
    // console.log('马上查询数据库，findcheckbox为:')
    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('zmyygzs_lxbx04').get({
      success: function (res) {
        // res.data 包含该记录的数据
        // console.log('findcheckbox为:', res.data.lxbx)
        // console.log('this.data.items为:', res.data.lxbx04)
        that.setData({
          lxbx04zhengzhuang: res.data.lxbx04,
        })
      }
    })
  },


  zmyygzs_lxbx: function (e) {
    // console.log('马上查询数据库，findcheckbox为:')
    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('zmyygzs_lxbx').get({
      success: function (res) {
        // res.data 包含该记录的数据
        // console.log('findcheckbox为:', res.data.lxbx)
        // console.log('this.data.items为:', that.data.items)
        that.setData({
          zmyygzs_lxbx: res.data.lxbx,
        })
      }
    })
  },
 
  sex_radio: function (e) {
    // console.log('马上查询数据库，findcheckbox为:')
    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('sex').get({
      success: function (res) {
        // console.log('马上查询数据库，findcheckbox为:', res.data.sex)
        that.setData({
          sex_radio: res.data.sex,
        })
      }
    })
  },
 





  findOpenid: function () {
    var that = this;
    const db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        db.collection('glry').where({
          _openid: app.globalData.openid
        })
          .get()
          .then(res => {
            // console.log('马上查询数据库，opneid为:', app.globalData.openid)
            if (res.data.length) {
              // this.setData({
              //   gzry_zqdd: res.data[0].zqaddress,
              //   gzry_name: res.data[0].name,
              //   // gzry_name: res.data[0].姓名,
              //   // name_phone: res.data[0].电话,               
              // })
              // personid = res.data[0]._id
              gzry_dep = res.data[0].dep
              gzry_zqdd = res.data[0].zqaddress
              gzry_name = res.data[0].name
              gzryphone = res.data[0].gzryphone
            }
            else {
              wx.showToast({
                title: '你是怎么来进的'
              })

            }
          })
      }
    })


  },
  lxbx: function (e) {
    let zmyygzs_lxbx = e.detail.value
    // console.log("checkbox-group", zmyygzs_lxbx)
    if (zmyygzs_lxbx != "") {
      // console.log("checkbox-true", zmyygzs_lxbx)

      this.setData({
        lxbx04zhengzhuangH: false,
        // lxbx04zhengzhuang: this.data.lxbx04zhengzhuang,
      
      })
    }
    else {
      this.lxbx04();
      this.setData({
        lxbx04zhengzhuangH: true,
      
      })
    }
  },
  add_mz_ms: function () {
    const db = wx.cloud.database()
    db.collection('info_zmyy').add({
      data: {
        name: name,
        sfz: sfz,
        phone: phone,
        
        gzry_memo: gzry_memo,
        form: "火车站测温台",
        input: "工作人员录入",
        gzryphone: gzryphone,
        gzry_addtime: util.formatTime(new Date()),
        gz_address: gzry_zqdd,
        gzry_dep: gzry_dep,
        gzry_name: gzry_name,
        twvalue: twvalue,
        frist_on: frist_on,
        sex: sex,
        address: address,
        lxbx: lxbx,
        lxbx04zhengzhuang: lxbx04zhengzhuang,
        lxbx04other1: lxbx04other1,
        workaddress: workaddress,
        workphone: workphone,
        startwhere: startwhere,
        crosswhere: crosswhere,
        fromwhere: fromwhere,
        traininfo: traininfo,
        shiyou: shiyou,

      }
      ,
      success: res => {
        // 在返回结果中会包含新创建的记录的 _id                  
        // wx.showToast({
        //   title: '新增记录成功',
        // })
        app.globalData.gzry_input_name = name
        app.globalData.gzry_input_twvalue = twvalue
      
        wx.switchTab({
          url: '/pages/info/hospital/hospital'
        })

      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '新增记录失败,请找其他人员登记！'
        })
        // console.error('[数据库] [新增记录] 失败：', err)
      }
    })
  },
  formSubmit: function (e) {
    // console.log('form发生了submit事件，携带数据为：', e.detail.value)
    // this.setData({
    //   btnEn: true,
    // })
    if (
      e.detail.value.name && e.detail.value.sfz && e.detail.value.sex && e.detail.value.address && e.detail.value.phone
      ) {
                  name= e.detail.value.name,
                  sfz= e.detail.value.sfz,
                  sex= e.detail.value.sex,
                  phone= e.detail.value.phone,
                  address= e.detail.value.address,
                  lxbx= e.detail.value.lxbx,
                  lxbx04zhengzhuang= e.detail.value.lxbx04zhengzhuang,
                    lxbx04other1 = e.detail.value.lxbx04other1,
                  workaddress= e.detail.value.workaddress,
                  workphone= e.detail.value.workphone,
                  startwhere= e.detail.value.startwhere,
                  crosswhere= e.detail.value.crosswhere,
                  fromwhere= e.detail.value.fromwhere,
                  traininfo= e.detail.value.traininfo,
                    gzry_memo = e.detail.value.gzry_memo,
                  shiyou= ticketv
      console.log('switch选择之前：', ticketv)
      switch (ticketv) {
        case "进出站":
          if (e.detail.value.startwhere && e.detail.value.fromwhere && e.detail.value.traininfo) { 
            this.zmyymz_twvalue_check();
          }
          else {
            wx.showToast({
              icon: 'loading',
              title: '请填全星号信息',
            })
          
          }
          break;
        case "买票":
          
          this.zmyymz_twvalue_check();
          
          break;
       
      }
      
     
    }
    else {
      // console.log('form发生了submit事件，携带数据为：', e.detail.value)
      wx.showToast({
        icon: 'loading',
        title: '请填全星号信息',
      })
     
    }
    
  },
  onInputEvent: function (e) {
    twvalue = e.detail.value
    // this.setData({
    //   returetwvalue: "  体温：" + twvalue
    // })
  },
  select_value: function (e) {
    select_value = e.detail.value
    // console.log('输入值：', select_value)
  },
})